package com.fan.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author D-BJ-144
 * @description SwaggerConfig
 * @Date 2024/10/30 16
 */
@Configuration
@EnableKnife4j
@EnableOpenApi
// @ConditionalOnProperty(value = "spring.profiles.active", havingValue = "dev")
public class SwaggerConfig {


    /**
     * 是否开启swagger
     */
    @Value("${swagger.enabled}")
    private boolean enabled;

    /**
     * 设置请求的统一前缀
     */
    @Value("${swagger.pathMapping}")
    private String pathMapping;

    @Value("${server.port}")
    private int port;

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .enable(enabled)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .pathMapping(pathMapping);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("平凡的脚步").description("简介")
                .termsOfServiceUrl("http://localhost:"+8080).version("0.0.1")
                .contact(new Contact("作者姓名", "", "邮箱")).build();
    }
}
